/*
 * main.c
 * 
 * GPIO测试程序的主入口
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // 显式包含unistd.h以解决usleep函数的隐式声明警告
#include "gpio_utils.h"

// 定义示例GPIO引脚号
// 注意：请根据您的实际硬件平台修改这些引脚号
#define GPIO_OUTPUT_EXAMPLE 17
#define GPIO_INPUT_EXAMPLE 18

int main(int argc, char *argv[])
{
    int i;
    key_state_t key_state;
    uint32_t start_time;
    
    // 消除未使用参数警告
    (void)argc;
    (void)argv;
    
    printf("GPIO Test Program Started\n");
    printf("Note: Please run this program with root privileges\n");
    printf("If your system's GPIO path is different, please modify the path definition in gpio_operations.c\n");
    
    // 初始化输出GPIO
    printf("Initializing GPIO %d as output mode...\n", GPIO_OUTPUT_EXAMPLE);
    if (gpio_init(GPIO_OUTPUT_EXAMPLE, "out") < 0) {
        fprintf(stderr, "Failed to initialize GPIO %d as output\n", GPIO_OUTPUT_EXAMPLE);
        return -1;
    }
    
    // 初始化输入GPIO
    printf("Initializing GPIO %d as input mode...\n", GPIO_INPUT_EXAMPLE);
    if (gpio_init(GPIO_INPUT_EXAMPLE, "in") < 0) {
        fprintf(stderr, "Failed to initialize GPIO %d as input\n", GPIO_INPUT_EXAMPLE);
        gpio_cleanup(GPIO_OUTPUT_EXAMPLE);
        return -1;
    }
    
    // 初始化按键状态
    key_init(&key_state, GPIO_INPUT_EXAMPLE);
    
    // 演示呼吸灯效果
    printf("Demonstrating various LED breathing effects...\n");
    
    // 平缓呼吸效果
    printf("Slow breathing effect...\n");
    led_breath(GPIO_OUTPUT_EXAMPLE, BREATH_EFFECT_SLOW, 3000);
    
    // 急促呼吸效果
    printf("Fast breathing effect...\n");
    led_breath(GPIO_OUTPUT_EXAMPLE, BREATH_EFFECT_FAST, 3000);
    
    // 脉冲效果
    printf("Pulse effect...\n");
    led_breath(GPIO_OUTPUT_EXAMPLE, BREATH_EFFECT_PULSE, 3000);
    
    // 波浪效果
    printf("Wave effect...\n");
    led_breath(GPIO_OUTPUT_EXAMPLE, BREATH_EFFECT_WAVE, 3000);
    
    // 关闭LED
    gpio_set_value(GPIO_OUTPUT_EXAMPLE, 0);
    
    // 演示按键检测功能
    printf("\nNow demonstrating key detection function:\n");
    printf("- Press the button briefly for short press event\n");
    printf("- Press and hold the button for long press event\n");
    printf("- Press twice quickly for double click event\n");
    printf("Test will run for 10 seconds...\n");
    
    start_time = get_system_time_ms();
    while (get_system_time_ms() - start_time < 10000) { // 测试10秒
        uint8_t event = key_detect(&key_state);
        
        // 根据按键事件执行不同操作
        switch (event) {
            case KEY_EVENT_SHORT_PRESS:
                printf("Short press event detected!\n");
                // 短按：闪烁一次
                gpio_set_value(GPIO_OUTPUT_EXAMPLE, 1);
                usleep(200000);
                gpio_set_value(GPIO_OUTPUT_EXAMPLE, 0);
                break;
            case KEY_EVENT_LONG_PRESS:
                printf("Long press event detected!\n");
                // 长按：常亮2秒
                gpio_set_value(GPIO_OUTPUT_EXAMPLE, 1);
                usleep(2000000);
                gpio_set_value(GPIO_OUTPUT_EXAMPLE, 0);
                break;
            case KEY_EVENT_DOUBLE_CLICK:
                printf("Double click event detected!\n");
                // 双击：闪烁三次
                for (i = 0; i < 3; i++) {
                    gpio_set_value(GPIO_OUTPUT_EXAMPLE, 1);
                    usleep(200000);
                    gpio_set_value(GPIO_OUTPUT_EXAMPLE, 0);
                    usleep(200000);
                }
                break;
            case KEY_EVENT_SIX_CLICK:
                printf("Six click event detected!\n");
                // 六击：呼吸灯效果
                led_breath(GPIO_OUTPUT_EXAMPLE, BREATH_EFFECT_SLOW, 2000);
                break;
            default:
                // 无事件，短暂延时降低CPU占用
                usleep(10000); // 10ms
                break;
        }
    }
    
    // 清理GPIO资源
    printf("\nCleaning up GPIO resources...\n");
    gpio_cleanup(GPIO_OUTPUT_EXAMPLE);
    gpio_cleanup(GPIO_INPUT_EXAMPLE);
    
    printf("GPIO test program completed\n");
    
    return 0;
}